package com.xsherl.leetcode.solution;

import java.util.*;

public class LSWithoutRepeatingChars {

    /**
     * 滑动窗口
     */
    public int lengthOfLongestSubstring(String s) {
        Set<Character> set = new HashSet<>();
        int len = s.length(), right = 0, max = 0;
        for (int i = 0; i < len; i++) {
            if (i > 0){
                set.remove(s.charAt(i - 1));
            }
            while (right < len){
                char c = s.charAt(right);
                if (set.contains(c)){
                    break;
                }
                right++;
                set.add(c);
            }
            max = Math.max(max, right - i);
        }
        return max;
    }

    public static void main(String[] args) {
        String s = "abcabcbb";
        int len = new LSWithoutRepeatingChars().lengthOfLongestSubstring(s);
        System.out.println(len);
    }
}
